﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Linq;
using BaseDll;
using CommonTools;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using StationDemo.设备信息.helper;
using Sunny.UI;
using UserData;
using static UserData.Control_ScrewData;

namespace StationDemo
{
    public partial class Form_Manual三角测试 : UIPage

    {
        string strStationName = "";
        public Form_Manual三角测试(string name)
        {
            strStationName = name;
            InitializeComponent();
        }
        private void Function_遍历控件(Control control)
        {
            foreach (var item in control.Controls)
            {
                if (item is Interface_UserControl)
                {
                    ((Interface_UserControl)item).Init();
                }
                else
                {
                    Function_遍历控件((Control)item);
                }
            }
        }

        private void Form_Manual_Load(object sender, EventArgs e)
        {
            Function_遍历控件(this);

            for (int i = (int)Station.三角测试1; i <= (int)Station.三角测试4; i++)
            {
                var item = (Station)i;
                if (item.ToString() != strStationName)
                    uiComboBox1.Items.Add(item.ToString());
            }

            uiLabel1.Text = strStationName;
        }

        private void uiButton1_Click(object sender, EventArgs e)
        {
            string text = uiComboBox1.Text;

            if (string.IsNullOrEmpty(text))
            {
                UIMessageBox.ShowError("请选择站点");
                return;
            }

            if (text == strStationName)
            {
                UIMessageBox.ShowError("不能选择当前站点");
                return;
            }

            if (Enum.TryParse(text, out Station station))
            {
                Station_三角测量 curStation = (Station_三角测量)StationMgr.GetInstance().GetStation(strStationName);
                Station_三角测量 targetStation = (Station_三角测量)StationMgr.GetInstance().GetStation(station.ToString());
                if (curStation != null && targetStation != null)
                {
                    curStation.station_Point = ClassCloneHelper.Clone(targetStation.station_Point);
                    Dictionary<string, PointInfo> staPoint_New = new Dictionary<string, PointInfo>();
                    System.Reflection.FieldInfo[] fieldInfos = curStation.station_Point.GetType().GetFields();
                    foreach (var field in fieldInfos)
                    {
                        var point = (PointInfo)field.GetValue(curStation.station_Point);

                        PointInfo point_New = ClassCloneHelper.Clone(point);
                        staPoint_New.Add(field.Name, point_New);
                    }
                    ConfigToolMgr.GetInstance().SaveStationPoint(strStationName, staPoint_New);

                    Dictionary<Form, Stationbase> dictionary = StationMgr.GetInstance().GetDicStaion();

                    if(dictionary != null)
                    {
                        foreach (var item in dictionary)
                        {
                            if(item.Value.Name == strStationName)
                            {
                                StationForm sf = item.Key as StationForm;
                                sf.UpdatadataGridView_PointInfoFormNewPoint(staPoint_New);
                                UIMessageBox.ShowSuccess("导入成功");
                                return;
                            }
                        }
                    }
                    UIMessageBox.ShowSuccess("导入失败");
                    //UIMessageBox.ShowSuccess("导入完成，请重启软件");
                }
                else
                {
                    UIMessageBox.ShowError("站点不存在或未初始化");
                }
            }
            else
            {
                UIMessageBox.ShowError("无效的站点名称");

            }
        }
    }
}
